#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web.Http;
using System.Web.Mvc;
using log4net;
using xConnected.Common.Language;
using xConnected.Core.Model;
using xConnected.Service;

#endregion

namespace xConnected.Web.Controllers
{
    public class LanguageController : ApiController
    {
        private readonly LanguageService _languageService;

        public LanguageController(LanguageService languageService)
        {
            _languageService = languageService;
        }


        public IEnumerable<Language> List()
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var languages = _languageService.GetAll();
                return languages;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        //[System.Web.Mvc.HttpPost]
        //public JsonResult Save(Language language)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        _languageService.SaveOrUpdate(language);
        //        return Json(language.Id, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //[System.Web.Mvc.HttpPost]
        //public JsonResult DeleteLanguage(int id)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var language = _languageService.GetById(id);
        //        _languageService.Delete(language);
        //        return Json("OK", JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message);
        //        return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //public JsonResult GetLanguagesAutocomplete(string searchTerm = "", int maxRows = 8)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var languages = _languageService.GetAll().ToList().OrderBy(l =>  LanguageRes.ResourceManager.GetString(l.Key,Thread.CurrentThread.CurrentCulture)).Select(l => new
        //        {
        //            Description = LanguageRes.ResourceManager.GetString(l.Key,Thread.CurrentThread.CurrentCulture),
        //            l.Id
        //        });
        //        if(searchTerm != "")
        //        {
        //            languages =
        //                languages.Where(l => l.Description.ToLower().Contains(searchTerm.ToLower())).Take(maxRows);
        //        }
        //        return Json(languages, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
        //    }
        //}
    }
}